﻿using NewRen.Helpers;
using NewRen.Log4net;
using NewRen.Models;
using NewRen.Models.DataModel;
using NewRen.Models.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NewRen.Controllers
{
    public class NewsController : BaseController
    {
        //新闻首页
        // GET: /Anecdote/
        //[HttpGet]
        public ActionResult Index()
        {
            try
            {
                var hotnews = db.News.OrderByDescending(a => a.Browses).Take(10).ToList();
                ViewBag.HotNews = hotnews;
                return View();
            }
            catch (Exception ex)
            {
                log.Error(new LogContent("新闻首页出错", HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=数据异常，请稍后重试");
            }
        }

        /// <summary>
        /// 分页获取新闻
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        public ActionResult GetNews(int page)
        {
            int index = page * 10;
            AjaxModel ajaxModel = new AjaxModel();
            List<News> news = new List<News>();
            List<vNews> _news = new List<vNews>();
            try
            {
                news = (from a in db.News
                             orderby a.DateTime descending
                             select a).Skip(index).Take(10).ToList();

                foreach (var item in news)
                {
                    _news.Add(new vNews(item));
                }
                if (_news.Count > 0)
                {
                    ajaxModel.Data = _news;
                    ajaxModel.Statu = "ok";
                    ajaxModel.Msg = "获取成功";
                }
                else
                {
                    ajaxModel.Data = _news;
                    ajaxModel.Statu = "err";
                    ajaxModel.Msg = "已加载完";
                }
            }
            catch (Exception ex)
            {
                ajaxModel.Data = _news;
                ajaxModel.Statu = "err";
                ajaxModel.Msg = "数据异常";
                log.Error(new LogContent("分页获取新闻出错", Helpers.HttpHelper.GetIPAddress()), ex);
            }
            return Json(ajaxModel, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        ///  展示新闻
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public ActionResult Show(int id)
        {
            try
            {
                var hotnews = db.News.OrderByDescending(a => a.Browses).Take(10).ToList();
                var news = db.News.Find(id);
                news.Browses = news.Browses + 1;
                ViewBag.HotNews = hotnews;
                db.SaveChanges();
                return View(news);
            }
            catch (Exception ex)
            {
                log.Error(new LogContent("展示新闻出错", Helpers.HttpHelper.GetIPAddress()), ex);
                return Redirect("/Shared/Info?msg=新闻不存在，请不要不合理操作");
            }
        }
    }
}